package com.lx.ngsl.core.exception;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lx.ngsl.core.dto.Rap;


/*
 * 全局异常处理器
 * 将对于控制器的全局配置放在同一个位置
 */
@ControllerAdvice
public class GlobalExceptionHandler {
	//getLogger(class)的参数用途:追踪产生此日志的类
	private Logger logger = LoggerFactory.getLogger(getClass());

	@ExceptionHandler(DuplicateKeyException.class)
	public Rap handleDuplicateKeyException(DuplicateKeyException e){
		logger.error(e.getMessage(), e);
		return Rap.error("数据库中已存在该记录");
	}

	/*
	 * 返回的是String 或者 Json数据，需要加上@ResponseBody
	 * 返回的是View则返回值是ModelAndView
	 */
//	@ExceptionHandler(Exception.class)
//	@ResponseBody
//	public Rap handleException(HttpServletRequest req,Exception e) {
//		return Rap.error();
//	}
	
}
